using System;
using System.Collections;

namespace InternetThermostat.Models
{
    public class Dictionary
    {
        ArrayList list = new ArrayList();

        public int Count { get { return list.Count; } }

        public void Add(DayTime key, byte value)
        {
            list.Add(new ValuePair(key, value));
        }


        public void AddOverride(DayTime key, byte value, byte minutes)
        {
            DayTime time = key;
            for (int i = 0; i < minutes; i++)
            {
                // Last override wins
                if (Schedule.Override.Contains(time))
                {
                    Schedule.Override.Remove(time);
                }

                list.Add(new ValuePair(time, value));
                time++;
            }
        }


        public byte this[DayTime key]
        {
            get
            {
                foreach (var item in list)
                {
                    if (((ValuePair)item).Key == key)
                        return ((ValuePair)item).Value;
                }

                throw new ArgumentOutOfRangeException("Key not found in dictionary");
            }
            set
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (((ValuePair)list[i]).Key == key)
                    {
                        ((ValuePair)list[i]).Value = value;
                        return;
                    }
                }

                throw new ArgumentOutOfRangeException("Key not found in dictionary");
            }
        }

        public bool Contains(DayTime key)
        {
            foreach (var item in list)
            {
                if (((ValuePair)item).Key == key)
                    return true;
            }

            return false;
        }

        public void Remove(DayTime key)
        {
            var filtered = new ArrayList();
            foreach (var item in list)
            {
                if (((ValuePair)item).Key != key)
                {
                    filtered.Add((ValuePair)item);
                }
            }

            list.Clear();
            list = (ArrayList)filtered.Clone();
        }

        public IEnumerator GetEnumerator()
        {
            return list.GetEnumerator();
        }

    }
}
